<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="角色名称" prop="Role_Name">
      <el-input v-model="ruleForm.Role_Name" />
    </el-form-item>

    <el-form-item label="状态" prop="Role_State">
      <el-radio-group v-model="ruleForm.Role_State">
        <el-radio :value="true">启用</el-radio>
        <el-radio :value="false">禁用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="权限" prop="permissionid">
      <el-tree
        style="max-width: 600px"
        :data="data"
        ref="treeRef"
        show-checkbox
        v-model="ruleForm.permissionid"
        node-key="id"
        :default-expanded-keys="[1,2,3,4,5]"
        :props="defaultProps"
      />
    </el-form-item>{{  }}
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        保存
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref,onMounted } from 'vue'
import { ElMessage, type ComponentSize, type ElTree, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios';
import { useRouter } from 'vue-router';

const router=useRouter()

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  Role_Name: '',
  Role_State: '',
  permissionid: [],
})


//数据验证
const rules = reactive<FormRules>({
  Role_Name: [
    { required: true, message: '角色名称不能为空', trigger: 'blur' },
    { min: 3, max: 10, message: '长度3-10个字符', trigger: 'blur' },
  ],
  Role_State: [
    {
      required: true,
      message: '请选择状态',
      trigger: 'change',
    },
  ],
})
//完成添加
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      console.log(treeRef.value?.getCheckedKeys(false))
      var perid=treeRef.value?.getCheckedKeys(false)
      axios.post("/api/User/AddPermissionInfo",ruleForm,{
        params:{
          perList:perid?.toString()
        }
      })
      .then(res=>{
        if(res.data.Data==1){
          ElMessage.success("添加成功")
          router.push("/ShowRolePermission")
        }else if(res.data.Data==-1){
          ElMessage.warning("角色名称已存在")
        }else{
          ElMessage.error("添加失败")
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
//重置按钮
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

//树形控件

onMounted(()=>{
  LoadPermission()
})
const defaultProps = {
  children: 'children',
  label: 'label',
}
const data = ref([{
    "id": 0,
    "label": "",
    "children": []
}])

//通过ref 引用ElTree 的实例 通过treref.value来获取实例
const treeRef=ref<InstanceType<typeof ElTree>>();
//加载权限信息
const LoadPermission=()=>{
  axios.get("/api/User/GetPermission?Pid=0")
  .then(res=>{
    data.value=res.data.Data
  })
}
</script>

